package com.kc.stand.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kc.stand.domain.Article;
import com.kc.stand.service.ArticleService;
import com.kc.stand.vo.ArticleVo;
import com.kc.sys.common.DataGridView;
import com.kc.sys.common.R;
import com.kc.sys.common.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 * 修改条文控制器端
 * @author 老杜
 * @since 2019-12-30
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     *加载修改条文左边的部门树结构Json
     */
    @RequestMapping("loadArticleManagerLeftJson")
    public DataGridView loadArticleManagerLeftJson(ArticleVo articleVo){
        List<Article> list = articleService.list();
        List<TreeNode> treeNodes = new ArrayList<>();
        for (Article article : list) {
            Boolean spread = article.getOpen()==1?true:false;
            treeNodes.add(new TreeNode(article.getId(),article.getPid(),article.getArticleTitle(),spread));
        }
        return new DataGridView(treeNodes);
    }
    //添加站细编规
    @RequestMapping("addArticle")
    public R addArticle(HttpServletRequest request, ArticleVo articleVo){
        String data = request.getParameter("data");
        JSONObject json = JSONObject.parseObject(data);
        Object parentId = json.get("parentId");
        int iparentId = Integer.parseInt(String.valueOf(parentId));
        Object context = json.get("context");
        String scontext = context.toString();
        try {
            articleVo.setOpen(1);
            articleVo.setPid(iparentId);
            articleVo.setArticleTitle(scontext);
            articleService.save(articleVo);
            return R.ok().message("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }

    //更新部门
    @RequestMapping("updateArticle")
    public R updateArticle(HttpServletRequest request,ArticleVo articleVo){
        String data = request.getParameter("data");
        JSONObject json = JSONObject.parseObject(data);
        Object nodeId = json.get("nodeId");
        int inodeId = Integer.parseInt(String.valueOf(nodeId));
        Object editNodeName = json.get("editNodeName");
        String snodeTitle = editNodeName.toString();
        try {
            articleVo.setId(inodeId);
            articleVo.setArticleTitle(snodeTitle);
            articleService.updateById(articleVo);
            return R.ok().message("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }

    @RequestMapping("updateArticleById/{nodeid}")
    public R updateArticleById(@PathVariable("nodeid") Integer id, ArticleVo articleVo){
        try {
            articleVo.setId(id);
            System.out.println("nodeid"+id);
            articleService.updateById(articleVo);
            return R.ok().message("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }
    /**
     * 根据Id 删除
     * */
    @RequestMapping("deleteArticle")
    public R deleteById(String  data){
        JSONObject json = JSONObject.parseObject(data);
        Object nodeId = json.get("nodeId");
        int inodeId = Integer.parseInt(String.valueOf(nodeId));
        try {
            articleService.removeById(inodeId);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    //检查当前部门有没有子节点（部门）
    @RequestMapping("checkArticleHasChildrenNode")
    public Map<String ,Object> checkArticleHasChildrenNode(ArticleVo articleVo){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",articleVo.getId());
        List<Article> list = articleService.list(queryWrapper);
        if(list.size()>0){
            map.put("value",true);
        }else {
            map.put("value",false);
        }
        return map;
    }

    //根据Id查询详情
    @RequestMapping("findArticleTree")
    public R findArticleTree(HttpServletRequest request) {
        String nodeId = request.getParameter("nodeId");
        Article article = articleService.getById(nodeId);
        System.out.println(article.getArticleContext());
        return R.ok().data("item",article);
    }
}

